package com.heima.user.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.pojos.ApUserRealname;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserAuthDto;
import com.heima.user.service.ApUserRealnameService;
import com.heima.user.mapper.ApUserRealnameMapper;
import org.springframework.stereotype.Service;

/**
 * @author 马毅爽
 * @description 针对表【ap_user_realname(APP实名认证信息表)】的数据库操作Service实现
 * @createDate 2023-12-17 20:30:56
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname>
        implements ApUserRealnameService {

    private static final Integer AUTH_PASS = 9;

    private static final Integer AUTH_FAIL = 2;

    @Override
    public ResponseResult listByDto(UserAuthDto dto) {
        // 1. 检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 获取参数
        Page<ApUserRealname> p = new Page<ApUserRealname>(dto.getPage(), dto.getSize()).addOrder(new OrderItem("created_time", false));
        // 3. 获取列表
        Page<ApUserRealname> page = lambdaQuery()
                .eq(dto.getStatus() != null, ApUserRealname::getStatus, dto.getStatus())
                .page(p);
        // 4. 返回结果
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        return result.ok(page.getRecords());
    }

    @Override
    public ResponseResult authPass(UserAuthDto dto) {
        // 1. 检查参数
        if (dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 修改状态
        lambdaUpdate()
                .set(ApUserRealname::getStatus, AUTH_PASS)
                .eq(ApUserRealname::getUserId, dto.getId())
                .update();

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult authFail(UserAuthDto dto) {
        // 1. 检查参数
        if (dto.getId() == null || StrUtil.isBlank(dto.getMsg())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 修改状态
        lambdaUpdate()
                .set(ApUserRealname::getStatus, AUTH_FAIL)
                .set(ApUserRealname::getReason, dto.getMsg())
                .eq(ApUserRealname::getUserId, dto.getId())
                .update();

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}




